package com.hibase.quartz.config.redis;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.hibase.quartz.constant.QuartzType;
import com.hibase.quartz.entity.MdsRedisJob;
import com.hibase.quartz.service.MdsJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RedisReceiver {

    @Autowired
    private MdsJobService mdsJobService;
    
    public void receiveMessage(String message) {

        log.info("接收到定时任务消息：{}",message);

        MdsRedisJob mdsRedisJob = JSON.parseObject(message, MdsRedisJob.class);

        QuartzType quartzType = QuartzType.valueOf(mdsRedisJob.getType());

        switch (quartzType) {

            case SAVEJOB:

                mdsJobService.saveMdsJob(mdsRedisJob.getMdsJob());

                break;

            case DELETJOB:

                mdsJobService.deleteMdsJob(mdsRedisJob.getMdsJob().getId());

                break;

            case RESTARTALLJOB:

                try {

                    mdsJobService.reStartAllJobs();

                } catch (SchedulerException e) {

                    e.printStackTrace();

                }

                break;

            case IMMEDIATEJOB:

                mdsJobService.runJob(mdsRedisJob.getMdsJob());

                break;
        }
    }
}